package ysu.sxm.ptjc.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.web.bind.annotation.*;
import ysu.sxm.ptjc.Service.SignService;
import ysu.sxm.ptjc.Service.UserService;
import ysu.sxm.ptjc.pojo.SignInfo;
import ysu.sxm.ptjc.pojo.UserInfo;

@RestController
@RequestMapping(path = "/operator",produces = "application/json; charset=utf-8")
    public class SignServlet {
    @Autowired
    SignService signService;
    @Autowired
    UserService userService;

    @RequestMapping(path = "/login",method = RequestMethod.POST)
    public String operatorLogin(@RequestBody UserInfo userInfo) throws JSONException {
        JSONObject returnjson = new JSONObject();
        int result=userService.findOperatorByString(userInfo.getUser_Id(),userInfo.getPassword());
        if(result==1){
            SignInfo signInfo=signService.Login(userInfo.getUser_Id());
            if(signInfo!=null){
                returnjson.put("result",200);
                returnjson.put("msg", "签到成功");
                returnjson.put("code", 0);
                returnjson.put("data", signInfo);
            }
            else{
                returnjson.put("result",500);
                returnjson.put("msg", "签到失败");
                returnjson.put("code", "0");
            }
        }
        else if(result==0){
            returnjson.put("result",404);
            returnjson.put("msg", "用户名或密码错误");
            returnjson.put("code", 0);
        }else {
            returnjson.put("result",404);
            returnjson.put("msg", "未知错误");
            returnjson.put("code", 0);
        }
        System.out.print("发送数据："+returnjson.toString());
        return returnjson.toJSONString();
    }

    @RequestMapping(path = "/logout",method = RequestMethod.POST)
    public String operatorLogout(@RequestBody SignInfo signInfo) throws JSONException {
        JSONObject returnjson = new JSONObject();
        if(signService.Logout(signInfo)){
            returnjson.put("result",200);
            returnjson.put("msg", "签退成功");
            returnjson.put("code", 5);
        }
        else{
            returnjson.put("result",500);
            returnjson.put("msg", "签退失败");
            returnjson.put("code", 5);
        }
        return returnjson.toJSONString();
    }



}